{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: JsonObjectConcept Extraction with References and Justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: JsonObjectConcept Extraction with References and Justifications\n",
        "\n",
        "import os\n",
        "from pprint import pprint\n",
        "from typing import Literal\n",
        "\n",
        "from contextgem import Document, DocumentLLM, JsonObjectConcept\n",
        "\n",
        "\n",
        "# Sample document text containing a customer complaint\n",
        "customer_complaint = \"\"\"\n",
        "CUSTOMER COMPLAINT #CR-2023-0472\n",
        "Date: November 15, 2023\n",
        "Customer: Sarah Johnson\n",
        "\n",
        "Description:\n",
        "I purchased the Ultra Premium Blender (Model XJ-5000) from your online store on October 3, 2023. The product was delivered on October 10, 2023. After using it only 5 times, the motor started making loud grinding noises and then completely stopped working on November 12.\n",
        "\n",
        "I've tried troubleshooting using the manual, including checking for obstructions and resetting the device, but nothing has resolved the issue. I expected much better quality given the premium price point ($249.99) and the 5-year warranty advertised.\n",
        "\n",
        "I've been a loyal customer for over 7 years and have purchased several kitchen appliances from your company. This is the first time I've experienced such a significant quality issue. I would like a replacement unit or a full refund.\n",
        "\n",
        "Previous interactions:\n",
        "- Spoke with customer service representative Alex on Nov 13 (Ref #CS-98721)\n",
        "- Was told to submit this formal complaint after troubleshooting was unsuccessful\n",
        "- No resolution offered during initial call\n",
        "\n",
        "Contact: sarah.johnson@example.com | (555) 123-4567\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document from the text\n",
        "doc = Document(raw_text=customer_complaint)\n",
        "\n",
        "# Create a JsonObjectConcept with justifications and references enabled\n",
        "complaint_analysis_concept = JsonObjectConcept(\n",
        "    name=\"Complaint analysis\",\n",
        "    description=\"Detailed analysis of a customer complaint\",\n",
        "    structure={\n",
        "        \"issue_type\": Literal[\n",
        "            \"product defect\",\n",
        "            \"delivery problem\",\n",
        "            \"billing error\",\n",
        "            \"service issue\",\n",
        "            \"other\",\n",
        "        ],\n",
        "        \"warranty_applicable\": bool,\n",
        "        \"severity\": Literal[\"low\", \"medium\", \"high\", \"critical\"],\n",
        "        \"customer_loyalty_status\": Literal[\"new\", \"regular\", \"loyal\", \"premium\"],\n",
        "        \"recommended_resolution\": Literal[\n",
        "            \"replacement\", \"refund\", \"repair\", \"partial refund\", \"other\"\n",
        "        ],\n",
        "        \"priority_level\": Literal[\"low\", \"standard\", \"high\", \"urgent\"],\n",
        "        \"expected_business_impact\": Literal[\"minimal\", \"moderate\", \"significant\"],\n",
        "    },\n",
        "    add_justifications=True,\n",
        "    justification_depth=\"comprehensive\",  # provide detailed justifications\n",
        "    justification_max_sents=10,  # provide up to 10 sentences for each justification\n",
        "    add_references=True,\n",
        "    reference_depth=\"sentences\",  # provide references to the sentences in the document\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([complaint_analysis_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept\n",
        "complaint_analysis_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Get the extracted complaint analysis\n",
        "complaint_analysis_item = complaint_analysis_concept.extracted_items[0]\n",
        "\n",
        "# Print the structured analysis\n",
        "print(\"Complaint Analysis\\n\")\n",
        "pprint(complaint_analysis_item.value)\n",
        "\n",
        "print(\"\\nJustification:\")\n",
        "print(complaint_analysis_item.justification)\n",
        "\n",
        "# Print key source references\n",
        "print(\"\\nReferences:\")\n",
        "for sent in complaint_analysis_item.reference_sentences:\n",
        "    print(f\"- {sent.raw_text}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}